package com.geniusk.system.controller;

import com.geniusk.common.core.JSONResult;
import com.geniusk.common.utils.PageUtil;
import com.geniusk.file.bo.DeleteFileByFileUrlBO;
import com.geniusk.file.bo.DownloadFileBO;
import com.geniusk.file.bo.SelectFileByPageBO;
import com.geniusk.file.bo.UploadFileBO;
import com.geniusk.file.core.File;
import com.geniusk.file.utils.FileUtil;
import com.geniusk.file.utils.MinioUtil;
import com.geniusk.security.annotation.PreFuncAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件请求处理
 */
@RestController
@RequestMapping("/system/file")
public class FileController {

    @Autowired
    private MinioUtil minioUtil;

    /**
     * 分页查询文件
     */
    @PostMapping("/selectFileByPage")
    @PreFuncAuth("SYS:FILE:SELECT")
    public JSONResult selectFileByPage(@Valid @RequestBody SelectFileByPageBO selectFileByPageBO) {
        int pageNum = selectFileByPageBO.getPageNum();
        int pageSize = selectFileByPageBO.getPageSize();
        List<File> list = minioUtil.selectFileList();
        Map<String,Object> result = PageUtil.getResultTableData(list,pageNum,pageSize);
        return JSONResult.ok(result);
    }

    /**
     * 获取预览地址
     */
    @GetMapping("/getPreviewUrl")
    public JSONResult getPreviewUrl(@RequestParam("fileName") String fileName) {
        Map<String, Object> result = new HashMap<>();
        result.put("previewUrl",minioUtil.preview(fileName));
        return JSONResult.ok(result);
    }

    /**
     * 文件上传请求
     */
    @PostMapping("upload")
    @PreFuncAuth("SYS:FILE:UPLOAD")
    public JSONResult upload( @RequestParam("file")  MultipartFile file) {
        try {
            // 上传并返回访问地址
            String url = minioUtil.uploadFile(file);
            File sysFile = new File();
            sysFile.setFileName(FileUtil.getName(url));
            sysFile.setFileUrl(url);
            Map<String,Object> result = new HashMap<>();
            result.put("sysFile",sysFile);
            return JSONResult.ok(result);
        } catch (Exception e) {
            return JSONResult.errorMsg(e.getMessage());
        }
    }

    /**
     * 获取直传地址
     */
    @PostMapping("/getUploadUrl")
    @PreFuncAuth("SYS:FILE:UPLOAD")
    public JSONResult getUploadUrl(@Valid @RequestBody UploadFileBO uploadFileBO){
        String originalFilename = uploadFileBO.getFileName();
        // 直传需要的url地址
        String objectName = minioUtil.extractFilename(originalFilename);
        String uploadUrl = minioUtil.getUploadUrl(objectName);
        Map<String,Object> result = new HashMap<>();
        result.put("uploadUrl",uploadUrl);
        result.put("fileUrl",objectName);
        return JSONResult.ok(result);
    }

    /**
     * 获取下载地址
     */
    @PostMapping("/getDownloadUrl")
    @PreFuncAuth("SYS:FILE:DOWNLOAD")
    public JSONResult getDownloadUrl(@Valid @RequestBody DownloadFileBO downloadFileBO){
        // 下载需要的url地址
        String downloadUrl = minioUtil.getDownloadUrl(downloadFileBO.getObjectName());
        Map<String,Object> result = new HashMap<>();
        result.put("downloadUrl",downloadUrl);
        return JSONResult.ok(result);
    }

    /**
     * 删除文件
     */
    @PostMapping("/deleteFileByObjectName")
    @PreFuncAuth("SYS:FILE:DELETE")
    public JSONResult deleteFileByObjectName(@Valid @RequestBody DeleteFileByFileUrlBO deleteFileByFileUrlBO) {
        minioUtil.deleteObject(deleteFileByFileUrlBO.getObjectName());
        return JSONResult.ok();
    }
}